//
//  ARKitManager.m
//  Charles
//
//  Created by Leo on 2019/8/13.
//  Copyright © 2019 charles. All rights reserved.
//

#import "ARKitManager.h"
#import "ARKitView.h"
#import <SceneKit/SceneKit.h>

@interface ARKitManager () {
    ARWorldTrackingConfiguration *_wordConfiguration;
    ARConfiguration *_faceConfiguration;
}

@end

@implementation ARKitManager

+ (instancetype)sharedInstance {
    static ARKitManager *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[self alloc]init];
    });
    return _manager;
}

- (void)startTrackingWithARView:(ARKitView *)arView trackingType:(ARTrackingConfigurationType)trackingType{
    switch (trackingType) {
        case ARTrackingConfigurationTypeDetectionImage:
            if (@available(iOS 11.3, *)) {
                self.worldConfiguration.detectionImages = [ARReferenceImage referenceImagesInGroupNamed:@"ARDetectionImageResource" bundle:nil];
                [arView.sceneView.session runWithConfiguration:self.worldConfiguration options:ARSessionRunOptionResetTracking | ARSessionRunOptionRemoveExistingAnchors];
            }
            break;
        case ARTrackingConfigurationTypePlaneDetection:
            if (@available(iOS 11.3, *)) {
                self.worldConfiguration.planeDetection = ARPlaneDetectionHorizontal;
                [arView.sceneView.session runWithConfiguration:self.worldConfiguration options:ARSessionRunOptionResetTracking | ARSessionRunOptionRemoveExistingAnchors];
            }
            break;
        case ARTrackingConfigurationTypeFaceTracking:
            if (@available(iOS 11.3, *)) {
                [arView.sceneView.session runWithConfiguration:self.faceConfiguration];
            }
            break;
        case ARTrackingConfigurationTypeFaceTrackingBlendShapes:
            if (@available(iOS 11.3, *)) {
                [arView.sceneView.session runWithConfiguration:self.faceConfiguration];
            }
            break;
        default:
            break;
    }
}

- (ARWorldTrackingConfiguration *)worldConfiguration {
    if (!_wordConfiguration) {
        _wordConfiguration = [[ARWorldTrackingConfiguration alloc]init];
    }
    return _wordConfiguration;
}

- (ARConfiguration *)faceConfiguration {
    if (!_faceConfiguration) {
        _faceConfiguration = [[ARFaceTrackingConfiguration alloc]init];
        _faceConfiguration.lightEstimationEnabled = YES;
    }
    return _faceConfiguration;
}


@end
